package cn.zebra.dev.common.utils;

import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串工具
 *
 * @author runnable@sina.cn
 */
public final class StrUtils {
    /**
     * 空格
     */
    public final static String SPACE = " ";
    public static final String NULL = "null";

    private static final String STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /**
     * 根据字符串长度随机生成字符串
     *
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            builder.append(STR.charAt(number));
        }
        return builder.toString();
    }

    /**
     * 判断字符串是否不为空
     *
     * @param cs 被判断的字符串
     * @return 如果为空则返回False 不为空返回True
     */
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    /**
     * 判断字符串是否为空
     *
     * @param cs 被判断的字符串
     * @return 如果为空则返回True 不为空返回False
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * UUID生成
     *
     * @return uuid
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }

    /**
     * 中文转unicode
     *
     * @param str 中文字符串
     * @return unicode转义后
     */
    public static String cnToUnicode(String str) {
        char[] chars = str.toCharArray();
        StringBuilder returnStr = new StringBuilder();
        for (char aChar : chars) {
            returnStr.append("\\u").append(Integer.toString(aChar, 16));
        }
        return returnStr.toString();
    }

    /**
     * 去除首尾指定字符
     *
     * @param str     字符串
     * @param element 指定字符
     */
    public static String trimFirstAndLastChar(String str, String element) {
        boolean beginIndexFlag;
        boolean endIndexFlag;
        do {
            int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
            int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
            str = str.substring(beginIndex, endIndex);
            beginIndexFlag = (str.indexOf(element) == 0);
            endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
        } while (beginIndexFlag || endIndexFlag);
        return str;
    }

    public static String getNonceString(int len) {
        String seed = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < len; i++) {
            tmp.append(seed.charAt(NumberUtils.getRandom(0, 61)));
        }
        return tmp.toString();
    }

    public static String getUTF8StringFromGBKString(String gbkStr) {
        return new String(getUTF8BytesFromGBKString(gbkStr), StandardCharsets.UTF_8);
    }

    public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
        int n = gbkStr.length();
        byte[] utfBytes = new byte[3 * n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            int m = gbkStr.charAt(i);
            if (m < 128 && m >= 0) {
                utfBytes[k++] = (byte) m;
                continue;
            }
            utfBytes[k++] = (byte) (0xe0 | (m >> 12));
            utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
            utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
        }
        if (k < utfBytes.length) {
            byte[] tmp = new byte[k];
            System.arraycopy(utfBytes, 0, tmp, 0, k);
            return tmp;
        }
        return utfBytes;
    }
}
